/* 
 * AUTHOR: Kevin Lam
 */

package com.apps.utils;

import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class FeedUtils {
	
	private static final String SECONDS_AGO = "<VALUE> second<s> ago";
	private static final String MINUTES_AGO = "<VALUE> minute<s> ago";
	private static final String HOURS_AGO = "<VALUE> hour<s> ago";
	private static final String DAYS_AGO = "<VALUE> day<s> ago";
	private static final String DATE_CURRENT_YEAR = "<MONTH> <DAY>";
	private static final String DATE_PAST_YEAR = "<MONTH> <DAY>, <YEAR>";
	
	private static final String[] MONTH_NAMES = {"January", "February", "March", "April", "May", "June","July","August","September","October","November","December"};
	
	public static String timeBetween(Date date){
		Calendar c_past = Calendar.getInstance(TimeZone.getTimeZone("PST"));
		Calendar c_current = Calendar.getInstance(TimeZone.getTimeZone("PST"));
		c_past.setTime(date);
		c_current.setTime(new Date());
		int d_year = c_current.get(Calendar.YEAR) - c_past.get(Calendar.YEAR);
		int d_month = c_current.get(Calendar.MONTH) - c_past.get(Calendar.MONTH);
		int d_day = c_current.get(Calendar.DAY_OF_MONTH) - c_past.get(Calendar.DAY_OF_MONTH);
		int d_hour = c_current.get(Calendar.HOUR_OF_DAY) - c_past.get(Calendar.HOUR_OF_DAY);
		int d_minute = c_current.get(Calendar.MINUTE) - c_past.get(Calendar.MINUTE);
		int d_second = c_current.get(Calendar.SECOND) - c_past.get(Calendar.SECOND);
		String out = "";
		if (d_year > 0) {
			out = DATE_PAST_YEAR.replace("<MONTH>",MONTH_NAMES[c_past.get(Calendar.MONTH)-1]);
			out = out.replace("<DAY>", "" + c_past.get(Calendar.DAY_OF_MONTH));
			out = out.replace("<YEAR>", "" + c_past.get(Calendar.YEAR));
			return out;
		} else if (d_month > 0) {
			out = DATE_CURRENT_YEAR.replace("<MONTH>",
					MONTH_NAMES[c_past.get(Calendar.MONTH)-1]);
			out = out.replace("<DAY>", "" + c_past.get(Calendar.DAY_OF_MONTH));
			return out;
		} else if (d_day > 0) {
			out = DAYS_AGO.replace("<VALUE>", "" + d_day);
			if (d_day > 1)
				out = out.replace("<s>", "s");
			else
				out = out.replace("<s>", "");
			return out;
		} else if (d_hour > 0) {
			out = HOURS_AGO.replace("<VALUE>", "" + d_hour);
			if (d_hour > 1)
				out = out.replace("<s>", "s");
			else
				out = out.replace("<s>", "");
			return out;
		} else if (d_minute > 0) {
			out = MINUTES_AGO.replace("<VALUE>", "" + d_minute);
			if (d_minute > 1)
				out = out.replace("<s>", "s");
			else
				out = out.replace("<s>", "");
			return out;
		} else if (d_second > 0) {
			out = SECONDS_AGO.replace("<VALUE>", "" + d_second);
			if (d_second > 1)
				out = out.replace("<s>", "s");
			else
				out = out.replace("<s>", "");
			return out;
		}
		return null;
	}

}
